extends Node2D
var a:int=0
var passed:bool=false
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	self.visible=false
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	pass


func _on_start_area_entered(area: Area2D) -> void:
	if not area.is_in_group("player"):return
	$Start.set_deferred("monitoring",false)
	Global.fighting=true
	self.visible=true
	$TestArea.set_collision_layer_value(1,true)
	%Camera.look_at_pos($CameraPos.global_position)
	Global.player_movment_mode.emit("suspend")
	Global.display_str.emit("试炼一\n来自黑暗中的凝视")
	await Global.display_string_finished
	start_test()
	pass # Replace with function body.
var ended:bool=true
func close_eye_too_long()->void:
	%Items._should_colse_eye=false
	Global.display_str.emit("过久的黑暗化为实质，将你吞噬")
	end_test()
	await Global.display_string_finished
	Global.player_should_death.emit()
var tween:Tween
func start_test()->void:
	ended=false
	$"../ColseEyeTimer".timeout.connect(close_eye_too_long)
	if tween and tween.is_running():return
	Global.display_tip.emit("鼠标点击冲刺")
	tween=create_tween()
	for child in $Content1.get_children():
		tween.tween_callback(child.open)
		tween.tween_property(self,"a",0,randf_range(1.2,3))
	tween.tween_property(self,"a",0,2)
	await tween.finished
	for child in $Content1.get_children():
		if not child.behitted:
			Global.player_should_death.emit()
			Global.display_str.emit("黑暗中的目光已将你吞噬")
			end_test()
			return
	%ColorFull.black()
	%Player.global_position=$Content3/Marker2D.global_position
	Global.player_movment_mode.emit("tie")
	Global.display_str.emit("你被黑暗中的一些眼睛惦记了")
	Global.display_str.emit("怨毒的目光投来时，闭上眼睛")
	Global.display_tip.emit("按 e 闭上眼睛")
	Global.display_str.emit("无视它们")
	await Global.display_string_finished
	%Items._can_close_eye=true
	for i in 3:
		await get_tree().create_timer(1).timeout
		if ended:return
		$Content3/ManyEye.play()
		await $Content3/ManyEye.played
		if ended:return
		if %Items._should_colse_eye==false:
			Global.display_str.emit("怨毒的目光化为实质，将你拖入黑暗")
			Global.player_animation_event.emit("surprise")
			await get_tree().create_timer(0.5).timeout
			Global.player_should_death.emit()
			end_test()
		else:
			$Content3/ManyEye.close()
			await $Content3/ManyEye.closed
			if ended:return
	%Items._can_close_eye=false
	
	await get_tree().create_timer(2).timeout
	Global.display_str.emit("你感到毛骨悚然")
	$Content2/Left.open_progress=1.7
	$Content2/Rigth.open_progress=1.7
	await get_tree().create_timer(2).timeout
	Global.player_movment_mode.emit("walk")
	Global.display_str.emit("不明的目光或将我们拖入黑暗")
	Global.display_str.emit("或给我们带来光明")
	passed=true
	end_test()
	#tween.tween_property($Content2/Left,"open_progress",1.8,0.8)
	#tween.parallel().tween_property($Content2/Rigth,"open_progress",1.8,0.8)
	#tween.tween_callback($Content3/ManyEye.play).set_delay(5)
func end_test()->void:
	ended=true
	if $"../ColseEyeTimer".timeout.is_connected(close_eye_too_long):
		$"../ColseEyeTimer".timeout.disconnect(close_eye_too_long)
	$Content3/ManyEye.close()
	$Content2/Left.open_progress=0
	$Content2/Rigth.open_progress=0
	if not passed:$Start.set_deferred("monitoring",true)
	self.visible=false
	$TestArea.set_collision_layer_value(1,false)
	%Camera.follow_player()
	%ColorFull.default()
	Global.fighting=false
